class ParamsTypeError(Exception):
    pass


def fib_with_for(n):
    try:
        n = int(n)
    except ValueError:
        raise ParamsTypeError('输入的不是整数！！！')
    if n < 0:
        raise ParamsTypeError('负数无法查找，请输入正整数！！！')
    if n in (0, 1):
        return n
    p, s = 1, 1
    for _ in range(n - 2):
        p, s = s, s + p
    return s


def fib_with_while(n):
    try:
        n = int(n)
    except ValueError:
        raise ParamsTypeError('输入的不是整数！！！')
    if n < 0:
        raise ParamsTypeError('负数无法查找，请输入正整数！！！')
    if n in (0, 1):
        return n
    p, s = 1, 1
    while n > 2:
        p, s = s, s + p
        n -= 1
    return s


if __name__ == '__main__':
    #  验证功能是否有问题，打印2种算法的前20个值
    for i in range(20):
        print(fib_with_for(i), end=' ')
    print()
    for i in range(20):
        print(fib_with_while(i), end=' ')
    while True:
        print()
        print('斐波拉契数列查询，输入q退出'.center(50, '='))
        num = input('请输入想要查询的斐波拉契数列索引：').strip()
        if num == 'q':
            exit()
        try:
            res_for = fib_with_for(num)
            res_while = fib_with_while(num)
        except ParamsTypeError as e:
            print(e.args[0])
            continue
        print(f'for 算出结果：{res_for}')
        print(f'while 算出结果：{res_while}')
